package com.cwnu.controller;

import com.cwnu.entity.Result;
import com.cwnu.entity.Comment;
import com.cwnu.entity.User;
import com.cwnu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 新增评论
    @PostMapping
    public Result createComment(String articleId, String content) {
//        System.out.println(articleId);
        commentService.createComment(Integer.parseInt(articleId), content);
        return Result.success();
    }
    // 根据文章id获取所有的评论
    @GetMapping
    public Result<List<Comment>> getCommentsByArticleId(@RequestParam Integer id) {
        List<Comment> comments = commentService.getCommentsByArticleId(id);
        return Result.success(comments);
    }
    // 根据评论id删除当前评论
    @DeleteMapping
    public Result deleteComment(@RequestParam Integer id) {
        commentService.deleteComment(id);
        return Result.success();
    }

    @GetMapping("/getUserByUserId")
    public Result getUserByUserId(@RequestParam Integer id) {
        User user = commentService.getUserByUserId(id);
        return Result.success(user);
    }
}
